<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
require('echarts/theme/macarons') // echarts theme

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      ntype: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler (val) {
        this.setOptions(val)
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy () {
    if (!this.chart) {
      return 
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions ({ expectedData, actualData } = {}) {
      this.chart.setOption({
        backgroundColor: '#ffffff',
        title: {
          text: '奶茶销售趋势',
          textStyle: {
            fontSize: 16,
            fontWeight: 'normal',
            color: '#303133'
          },
          left: 'center',
          top: 0
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          },
          padding: [12, 16],
          backgroundColor: 'rgba(255, 255, 255, 0.9)',
          borderColor: '#f5f5f5',
          borderWidth: 1,
          textStyle: {
            color: '#666'
          }
        },
        legend: {
          data: ['计划', '实际'],
          icon: 'rect',
          itemWidth: 10,
          itemHeight: 10,
          itemGap: 20,
          bottom: 0,
          textStyle: {
            color: '#666',
            fontSize: 12
          }
        },
        grid: {
          left: 20,
          right: 20,
          bottom: 40,
          top: 50,
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
          axisTick: {
            show: false
          },
          axisLine: {
            lineStyle: {
              color: '#e6e6e6'
            }
          },
          axisLabel: {
            color: '#666',
            fontSize: 12
          }
        },
        yAxis: {
          type: 'value',
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            color: '#666',
            fontSize: 12
          },
          splitLine: {
            lineStyle: {
              color: '#ebeef5',
              type: 'dashed'
            }
          }
        },
        series: [
          {
            name: '计划',
            type: 'line',
            smooth: true,
            symbol: 'circle',
            symbolSize: 6,
            showSymbol: true,
            lineStyle: {
              normal: {
                width: 3,
                color: '#409EFF'
              }
            },
            itemStyle: {
              normal: {
                color: '#409EFF',
                borderColor: '#fff',
                borderWidth: 2
              }
            },
            areaStyle: {
              normal: {
                color: {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'rgba(64, 158, 255, 0.3)'
                    },
                    {
                      offset: 1,
                      color: 'rgba(64, 158, 255, 0.05)'
                    }
                  ]
                }
              }
            },
            data: expectedData,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
          {
            name: '实际',
            type: 'line',
            smooth: true,
            symbol: 'circle',
            symbolSize: 6,
            showSymbol: true,
            lineStyle: {
              normal: {
                width: 3,
                color: '#36A3F7'
              }
            },
            itemStyle: {
              normal: {
                color: '#36A3F7',
                borderColor: '#fff',
                borderWidth: 2
              }
            },
            areaStyle: {
              normal: {
                color: {
                  type: 'linear',
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'rgba(54, 163, 247, 0.3)'
                    },
                    {
                      offset: 1,
                      color: 'rgba(54, 163, 247, 0.05)'
                    }
                  ]
                }
              }
            },
            data: actualData,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }
        ]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.chart {
  position: relative;
  width: 100%;
  height: 100%;
}
</style>
